package com.android.fragmentdemo.util;
import java.util.ArrayList;
import java.util.List;

public class StepLoadDataUtil<T> {
    private int step = 0;
    private int initLoadCount = 0;
    private int loadCountPerTime = 0;
    private boolean loadFinished = false;
    private List<T> mData;
    private List<T> mInitDataSet;

    public void init(List<T> mDataSet, int initLoadCount) {
        this.mData = new ArrayList<>();
        this.mData.addAll(mDataSet);
        mInitDataSet = mDataSet.subList(0, Math.min(initLoadCount, mDataSet.size()));
        loadFinished = initLoadCount >= mDataSet.size();
        this.initLoadCount = initLoadCount;
    }

    public List<T> getInitDataSet() {
        return mInitDataSet;
    }

    public List<T> loadItem() {
        int startIndex = step * loadCountPerTime + initLoadCount;
        int endIndex = Math.min(startIndex + loadCountPerTime, mData.size());
        loadFinished = endIndex >= mData.size();
        step++;
        return mData.subList(startIndex, endIndex);
    }

    public boolean hadLoadFinished() {
        return loadFinished;
    }

    public void updateLoadCountPerTime(int loadCountPerTime) {
        this.loadCountPerTime = loadCountPerTime;
    }

    public void reset() {
        loadFinished = false;
        step = 0;
    }
}
